package com.dotflat.docs.service.impl;

import com.dotflat.docs.domain.Document;
import com.dotflat.docs.domain.DocumentDirectory;
import com.dotflat.docs.po.DocumentPO;
import com.dotflat.docs.repository.DocumentRepository;
import com.dotflat.docs.service.DocumentDirectoryService;
import com.dotflat.docs.service.DocumentService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class DocumentServiceImpl extends AreableObjectBaseServiceImpl<Document, DocumentRepository> implements DocumentService {

    @Autowired
    @Override
    public void setBaseRepository(DocumentRepository repository){
        super.setBaseRepository(repository);
    }

    @Autowired
    private DocumentDirectoryService directoryService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Document add(String areaId, String staffId, AreableObjectBasePO param) {
        if (!(param instanceof DocumentPO)){
            return super.add(areaId, staffId, param);
        }
        DocumentPO po = ((DocumentPO)param);
        String dirId = po.getDirId();

        DocumentDirectory dir = directoryService.get(areaId, staffId, dirId);

        Document ret = add(areaId, staffId, param, o->{

            o.setDirTitle(dir.getTitle());
            o.setDirType(dir.getType());
            o.setStaffName(((DocumentPO) param).getStaffName());
            o.setStaffHeadImg(((DocumentPO) param).getStaffHeadImg());

        });

        Long itemCount = baseRepository.countByAreaIdAndDirTypeAndDirIdAndDeletedIsFalse(areaId,dir.getType(),dir.getId());
        dir.setItemCount(Math.toIntExact(itemCount) +1);

        directoryService.add(dir);

        return ret;
    }

    @Override
    public void delete(String areaId, String staffId, AreableObjectBasePO param) {

        DocumentPO po = new DocumentPO();
        po.setId(param.getId());
        Document d  = get(areaId, staffId,po.getId());
        DocumentDirectory dir = directoryService.get(areaId, staffId, d.getDirId());
        super.delete(areaId, staffId, po);
        Long itemCount = baseRepository.countByAreaIdAndDirTypeAndDirIdAndDeletedIsFalse(areaId,dir.getType(),dir.getId());
        if (itemCount>0){
            itemCount = itemCount-1;
        }
        dir.setItemCount(Math.toIntExact(itemCount));
        directoryService.add(dir);
    }

    @Override
    @Transactional
    public void delete(String areaId, String staffId, String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Document d  = get(areaId, staffId,ids[i]);
            DocumentDirectory dir = directoryService.get(areaId, staffId, d.getDirId());
            Long itemCount = baseRepository.countByAreaIdAndDirTypeAndDirIdAndDeletedIsFalse(areaId,dir.getType(),dir.getId());
            if (itemCount>0){
                itemCount = itemCount-1;
            }
            dir.setItemCount(Math.toIntExact(itemCount));
            directoryService.add(dir);
        }
        super.delete(areaId, staffId, ids);
    }


}